﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using System.IO;
using NPOI.HSSF.UserModel;
using Zwms.Client.InfoSys;

namespace Zwms.Client.InWms
{
    public partial class InPlanBillWnd : UserControl
    {
        #region 成员变量
        private bool _bEditable = false;
        private string _sCurrentInPlanBillCode = "";
        #endregion 

        #region 构造函数
        public InPlanBillWnd(MainForm mainform)
        {
            InitializeComponent();
            InitBtnImage();
            InitControl();

            btnStart.Click += (a, b) =>
                {
                    if (string.IsNullOrWhiteSpace(_sCurrentInPlanBillCode))
                    {
                        MessageBox.Show("请首先选择入库计划单！");
                        return;
                    }

                    
                    BilInPlanBill record = inPlanBillUC.GetBill();
                    ServiceReferenceZwms.ServiceZwmsClient client
                        = new ServiceReferenceZwms.ServiceZwmsClient();
                    if (record.BillStatus == "0")
                    {
                        if (MessageBox.Show("进入收货阶段入库计划单将无法修改，是否继续？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                        {
                            return;
                        }
                        record.BillStatus = "1";
                        if (!client.UpdateInPlanBill(record))
                        {
                            MessageBox.Show("入库计划单状态更新失败！");
                            return;
                        }
                    }

                    //首先判断状态
                    switch (record.BillStatus)
                    {
                        case "0":
                        case "1":
                            InWms.InBillWnd window = new InBillWnd(_sCurrentInPlanBillCode);
                            mainform.AddTablePage("扫描收货", window, true);
                            break;

                        case "2":
                            MessageBox.Show("该入库计划单已入库");
                            break;
                        case "3":
                            MessageBox.Show("该入库计划单已撤销！");
                            break;
                    }
                    LoadInPlanBill(_sCurrentInPlanBillCode);
                };
        }

        public InPlanBillWnd(MainForm mainform,string inPlanBillCode)
        {
            InitializeComponent();
            this._sCurrentInPlanBillCode = inPlanBillCode;
            this.LoadInPlanBill(this._sCurrentInPlanBillCode);
            this.InitBtnImage();
            this.InitControl();

            btnStart.Click += (a, b) =>
            {
                if (string.IsNullOrWhiteSpace(_sCurrentInPlanBillCode))
                {
                    MessageBox.Show("请首先选择入库计划单！");
                    return;
                }


                BilInPlanBill record = inPlanBillUC.GetBill();
                ServiceReferenceZwms.ServiceZwmsClient client
                    = new ServiceReferenceZwms.ServiceZwmsClient();
                if (record.BillStatus == "0")
                {
                    if (MessageBox.Show("进入收货阶段入库计划单将无法修改，是否继续？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    {
                        return;
                    }
                    record.BillStatus = "1";
                    if (!client.UpdateInPlanBill(record))
                    {
                        MessageBox.Show("入库计划单状态更新失败！");
                        return;
                    }
                }

                //首先判断状态
                switch (record.BillStatus)
                {
                    case "0":
                    case "1":
                        InWms.InBillWnd window = new InBillWnd(_sCurrentInPlanBillCode);
                        mainform.AddTablePage("扫描收货", window, true);
                        break;

                    case "2":
                        MessageBox.Show("该入库计划单已入库");
                        break;
                    case "3":
                        MessageBox.Show("该入库计划单已撤销！");
                        break;
                }
            };
        }

        #endregion

        #region 私有函数

        private void InitBtnImage()
        {
            this.btnInput.Image = global::Zwms.Client.Properties.Resources.addlist;
            this.btnNewInPlan.Image = global::Zwms.Client.Properties.Resources.add;
            this.btnEditInPlan.Image = global::Zwms.Client.Properties.Resources.edit;
            this.btnQueryInPlan.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnAddInPlanDetail.Image = global::Zwms.Client.Properties.Resources.addall;
            this.btnEditInPlanDetail.Image = global::Zwms.Client.Properties.Resources.editall;
            this.btnDelInPlanDetail.Image = global::Zwms.Client.Properties.Resources.removeall;
            this.btnSave.Image = global::Zwms.Client.Properties.Resources.save;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;

            this.btnRefresh.Image = global::Zwms.Client.Properties.Resources.refresh;
            this.btnPrint.Image = global::Zwms.Client.Properties.Resources.print;
            this.btnOutput.Image = global::Zwms.Client.Properties.Resources.list;
            this.btnPrintBarCode.Image = global::Zwms.Client.Properties.Resources.barcode;
            this.btnStart.Image = global::Zwms.Client.Properties.Resources.check;
            this.btnCancelBill.Image = global::Zwms.Client.Properties.Resources.cancel2;
        }

        private void InitControl()
        {
            //控制状态
            SetEditState(false);
            inPlanBillUC.SetEditState(false);
        }

        private void SetEditState(bool b)
        {
            btnNewInPlan.Enabled = !b;
            btnEditInPlan.Enabled = !b;
            btnQueryInPlan.Enabled = !b;
            btnAddInPlanDetail.Enabled = b;
            btnEditInPlanDetail.Enabled = b;
            btnDelInPlanDetail.Enabled = b;
            btnSave.Enabled = b;
            btnCancel.Enabled = b;
        }

        private void LoadInPlanBill(string sCode)
        {
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInPlanBill record = new BilInPlanBill();
            record.Id = sCode;
            record = client.QueryInPlanBill(record);
            if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
            {
                //无限制
            }
            else
            {
                //判断是否为该用户的入库计划单
                if (record.CreateUserCode != MainForm.CurrentUser.Id)
                {
                    MessageBox.Show("对不起，您不具备该入库单的操作权限！");
                    return;
                }
            }
            this.inPlanBillUC.LoadBill(sCode);
            this.inPlanBillDetailUC.LoadInPlanBillDetail(sCode);
        }

        #endregion

        #region 控件函数
        private void btnNewInPlan_Click(object sender, EventArgs e)
        {
            NewInplanBillForm window = new NewInplanBillForm();
            window.ShowDialog();
        }

        private void btnEditInPlan_Click(object sender, EventArgs e)
        {
            if (this._sCurrentInPlanBillCode == "")
            {
                MessageBox.Show("请首先选择入库计划单！");
                return;
            }

            if (this.inPlanBillUC.GetBill().BillStatus != "0")
            {
                MessageBox.Show("只允许编辑订单状态为“等待入库”的计划单！");
                return;
            }

            _bEditable = true;
            SetEditState(true);
            inPlanBillUC.SetEditState(true);
        }

        private void btnQueryInPlan_Click(object sender, EventArgs e)
        {
            QueryInPlanBillWnd uc = new QueryInPlanBillWnd();
            ContainerForm window = new ContainerForm(uc, "选择入库计划单");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //判断权限
                if (MainForm.CurrentUser.UserRole.Trim() == "01" || MainForm.CurrentUser.UserRole.Trim() == "02")
                {
                    //无限制
                }
                else if (MainForm.CurrentUser.UserRole.Trim() == "03")
                {
                    //判断是否为该用户的入库计划单
                    if (uc.SelectedRecord.CreateUserCode != MainForm.CurrentUser.Id)
                    {
                        MessageBox.Show("对不起，您不具备该入库计划单的操作权限！");
                        return;
                    }
                }

                this._sCurrentInPlanBillCode = uc.SelectedRecord.Id;
                LoadInPlanBill(this._sCurrentInPlanBillCode);
            }
        }

        private void btnAddInPlanDetail_Click(object sender, EventArgs e)
        {
            if (this._sCurrentInPlanBillCode == "")
            {
                MessageBox.Show("请首先选择入库计划单！");
                return;
            }

            InPlanBillDetailForm window 
                = new InPlanBillDetailForm(EnumOpenType.New, 
                    this._sCurrentInPlanBillCode,string.Empty,string.Empty);
            if (window.ShowDialog() == DialogResult.OK)
            {
                LoadInPlanBill(this._sCurrentInPlanBillCode);
            }

        }

        private void btnEditInPlanDetail_Click(object sender, EventArgs e)
        {
            if (this.inPlanBillDetailUC.CurrentSelectInPlanBillDetail == null)
            {
                MessageBox.Show("请首先选择入库计划明细！");
                return;
            }

            InPlanBillDetailForm window 
                = new InPlanBillDetailForm(EnumOpenType.Open, 
                    this._sCurrentInPlanBillCode, 
                    this.inPlanBillDetailUC.CurrentSelectInPlanBillDetail.GoodsCode, 
                    this.inPlanBillDetailUC.CurrentSelectInPlanBillDetail.GoodsBatch);
            if (window.ShowDialog() == DialogResult.OK)
            {
                LoadInPlanBill(this._sCurrentInPlanBillCode);
            }
        }

        private void btnDelInPlanDetail_Click(object sender, EventArgs e)
        {
            if (this.inPlanBillDetailUC.CurrentSelectInPlanBillDetail == null)
            {
                MessageBox.Show("请首先选择入库计划明细！");
                return;
            }

            if (MessageBox.Show("是否确认删除所选择的项？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            if (client.DeleteInPlanBillDetail(this.inPlanBillDetailUC.CurrentSelectInPlanBillDetail))
            {
                MessageBox.Show("成功！");
                LoadInPlanBill(this._sCurrentInPlanBillCode);
            }
            else
            {
                MessageBox.Show("失败！");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_bEditable == false)
            {
                return;
            }

            BilInPlanBill tempRecord = this.inPlanBillUC.GetBill();
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            if (client.UpdateInPlanBill(tempRecord))
            {
                SysLogWriter.WriteOptLog("入库计划单", _sCurrentInPlanBillCode, "改");
                MessageBox.Show("成功！");
                SetEditState(false);
                inPlanBillUC.SetEditState(false);
                _bEditable = false;
            }
            else
            {
                MessageBox.Show("失败！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            LoadInPlanBill(this._sCurrentInPlanBillCode);

            SetEditState(false); 
            inPlanBillUC.SetEditState(false);
            _bEditable = false;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (this._sCurrentInPlanBillCode != "")
            {
                LoadInPlanBill(this._sCurrentInPlanBillCode);
            }
        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(_sCurrentInPlanBillCode))
            {
                MessageBox.Show("请首先选择入库计划单！");
                return;
            }
            
            SaveFileDialog sfDlg = new SaveFileDialog();
            sfDlg.Filter = "*.xls|*.xls";
            if (sfDlg.ShowDialog() == DialogResult.OK)
            {
                string sTemplate = System.Windows.Forms.Application.StartupPath + @"\Template\入库计划单模板.xls";
                if (!File.Exists(sTemplate))
                {
                    MessageBox.Show("模板文件不存在！");
                    return;
                }
                string sOutputDir = Path.GetDirectoryName(sfDlg.FileName);
                //打开文件
                FileStream fileTemplate = new FileStream(sTemplate, FileMode.Open, FileAccess.Read);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(fileTemplate);
                //获取工作簿
                HSSFSheet sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet;

                ServiceReferenceZwms.ServiceZwmsClient client
                    = new ServiceReferenceZwms.ServiceZwmsClient();
                //填写入库计划单基本信息
                BilInPlanBill inPlanBill = new BilInPlanBill();
                inPlanBill.Id = _sCurrentInPlanBillCode;
                inPlanBill = client.QueryInPlanBill(inPlanBill);
                sheet.CreateRow(1);
                sheet.CreateRow(2);
                sheet.CreateRow(3);
                sheet.CreateRow(4);
                sheet.CreateRow(5);
                sheet.GetRow(1).CreateCell(0);
                sheet.GetRow(1).CreateCell(1);
                sheet.GetRow(1).CreateCell(2);
                sheet.GetRow(1).CreateCell(3);
                sheet.GetRow(1).CreateCell(4);
                sheet.GetRow(1).CreateCell(5);
                sheet.GetRow(1).CreateCell(6);
                sheet.GetRow(1).CreateCell(7);
                sheet.GetRow(1).CreateCell(8);
                sheet.GetRow(1).CreateCell(9);
                sheet.GetRow(1).CreateCell(10);
                sheet.GetRow(2).CreateCell(0);
                sheet.GetRow(2).CreateCell(1);
                sheet.GetRow(2).CreateCell(2);
                sheet.GetRow(2).CreateCell(3);
                sheet.GetRow(2).CreateCell(4);
                sheet.GetRow(2).CreateCell(5);
                sheet.GetRow(2).CreateCell(6);
                sheet.GetRow(2).CreateCell(7);
                sheet.GetRow(2).CreateCell(8);
                sheet.GetRow(2).CreateCell(9);
                sheet.GetRow(2).CreateCell(10);
                sheet.GetRow(3).CreateCell(0);
                sheet.GetRow(3).CreateCell(1);
                sheet.GetRow(3).CreateCell(2);
                sheet.GetRow(3).CreateCell(3);
                sheet.GetRow(3).CreateCell(4);
                sheet.GetRow(3).CreateCell(5);
                sheet.GetRow(3).CreateCell(6);
                sheet.GetRow(3).CreateCell(7);
                sheet.GetRow(3).CreateCell(8);
                sheet.GetRow(3).CreateCell(9);
                sheet.GetRow(3).CreateCell(10);
                sheet.GetRow(4).CreateCell(0);
                sheet.GetRow(4).CreateCell(1);
                sheet.GetRow(4).CreateCell(2);
                sheet.GetRow(4).CreateCell(3);
                sheet.GetRow(4).CreateCell(4);
                sheet.GetRow(4).CreateCell(5);
                sheet.GetRow(4).CreateCell(6);
                sheet.GetRow(4).CreateCell(7);
                sheet.GetRow(4).CreateCell(8);
                sheet.GetRow(4).CreateCell(9);
                sheet.GetRow(4).CreateCell(10);
                sheet.GetRow(5).CreateCell(0);
                sheet.GetRow(5).CreateCell(1);
                sheet.GetRow(5).CreateCell(2);
                sheet.GetRow(5).CreateCell(3);
                sheet.GetRow(5).CreateCell(4);
                sheet.GetRow(5).CreateCell(5);
                sheet.GetRow(5).CreateCell(6);
                sheet.GetRow(5).CreateCell(7);
                sheet.GetRow(5).CreateCell(8);
                sheet.GetRow(5).CreateCell(9);
                sheet.GetRow(5).CreateCell(10);


                sheet.GetRow(1).GetCell(0).SetCellValue("采购单号:");
                sheet.GetRow(1).GetCell(2).SetCellValue(inPlanBill.POCode);
                sheet.GetRow(1).GetCell(5).SetCellValue("发货单号:");
                sheet.GetRow(1).GetCell(6).SetCellValue(inPlanBill.SendBillCode);

                sheet.GetRow(2).GetCell(0).SetCellValue("采购日期：");
                sheet.GetRow(2).GetCell(5).SetCellValue("发货仓库：");

                sheet.GetRow(3).GetCell(0).SetCellValue("入库日期:");
                sheet.GetRow(3).GetCell(2).SetCellValue(inPlanBill.PlanReceiveDate);
                sheet.GetRow(3).GetCell(5).SetCellValue("供应商代码:");
                sheet.GetRow(3).GetCell(6).SetCellValue(inPlanBill.SenderCode);

                sheet.GetRow(4).GetCell(0).SetCellValue("收货人：");
                sheet.GetRow(4).GetCell(3).SetCellValue("电话：");
                sheet.GetRow(4).GetCell(5).SetCellValue("供应商名称:");
                sheet.GetRow(4).GetCell(6).SetCellValue(inPlanBill.SenderName);
                sheet.GetRow(4).GetCell(10).SetCellValue("订单总金额：");

                sheet.GetRow(5).GetCell(0).SetCellValue("审核人：");
                sheet.GetRow(5).GetCell(5).SetCellValue("供应商联系人：");
                sheet.GetRow(5).GetCell(10).SetCellValue("电话：");

                int iRow = 8;
                int i = 1;
                BilInPlanBillDetail queryDetail = new BilInPlanBillDetail();
                queryDetail.IBPlanCode = _sCurrentInPlanBillCode;
                PagerIndex pIndex= new PagerIndex(1,int.MaxValue);
                IList<BilInPlanBillDetail> inPlanBillDetailList = client.QueryInPlanBillDetailList(queryDetail, ref pIndex);
                foreach (BilInPlanBillDetail record in inPlanBillDetailList)
                {
                    sheet.CreateRow(iRow);
                    sheet.GetRow(iRow).CreateCell(0);
                    sheet.GetRow(iRow).CreateCell(1);
                    sheet.GetRow(iRow).CreateCell(2);
                    sheet.GetRow(iRow).CreateCell(3);
                    sheet.GetRow(iRow).CreateCell(4);
                    sheet.GetRow(iRow).CreateCell(5);
                    sheet.GetRow(iRow).CreateCell(6);
                    sheet.GetRow(iRow).CreateCell(7);
                    sheet.GetRow(iRow).CreateCell(8);
                    sheet.GetRow(iRow).CreateCell(9);
                    sheet.GetRow(iRow).CreateCell(10);
                    sheet.GetRow(iRow).CreateCell(11);

                    sheet.GetRow(iRow).GetCell(0).SetCellValue(i.ToString());
                    sheet.GetRow(iRow).GetCell(1).SetCellValue(record.GoodsCode.Trim());
                    sheet.GetRow(iRow).GetCell(2).SetCellValue(record.GoodsName.Trim());
                    sheet.GetRow(iRow).GetCell(5).SetCellValue(record.GoodsSpec.ToString());
                    sheet.GetRow(iRow).GetCell(6).SetCellValue(record.GoodsPackageQty);
                    sheet.GetRow(iRow).GetCell(7).SetCellValue(record.GoodsPieceQty);
                    sheet.GetRow(iRow).GetCell(8).SetCellValue(record.GoodsQty);
                    sheet.GetRow(iRow).GetCell(11).SetCellValue(record.PiecePrice.ToString());
                    iRow++;
                    i++;
                }
                sheet.ForceFormulaRecalculation = true;

                //保存文件
                try
                {
                    FileStream fileOutput = new FileStream(sfDlg.FileName, FileMode.Create);
                    hssfworkbook.Write(fileOutput);
                    fileOutput.Close();

                    System.Diagnostics.Process.Start(sOutputDir);
                }
                catch (IOException ex)
                {
                    MessageBox.Show("请先关闭已打开的Excel文件。");
                    return;
                }
                finally
                {
                    Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(_sCurrentInPlanBillCode))
            {
                MessageBox.Show("请首先选择入库计划单！");
                return;
            }

            BilInPlanBill inPlanBill = this.inPlanBillUC.GetBill();
            IList<BilInPlanBillDetail> inPlanBillDetailList = inPlanBillDetailUC.GetBillDetail();
            Report.ReportHelper.PrintInPlanBill(inPlanBill, inPlanBillDetailList.ToList());
        }

        private void btnCancelBill_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(_sCurrentInPlanBillCode))
            {
                MessageBox.Show("请首先选择入库计划单！");
                return;
            }

            if (MessageBox.Show(string.Format("是否确认撤销入库计划单 {0}？", _sCurrentInPlanBillCode.Trim()), "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }

            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            BilInPlanBill inPlanBill = this.inPlanBillUC.GetBill();
            inPlanBill.BillStatus = "3";
            if (client.UpdateInPlanBill(inPlanBill))
            {
                _sCurrentInPlanBillCode = string.Empty;
                inPlanBillUC.Clear();
                inPlanBillDetailUC.Clear();
                MessageBox.Show("成功！");
            }

        }
      

        private void btnPrintBarCode_Click(object sender, EventArgs e)
        {
           
            if (string.IsNullOrWhiteSpace(_sCurrentInPlanBillCode))
            {
                MessageBox.Show("请首先选择入库计划单！");
                return;
            }
            if (MessageBox.Show(string.Format("是否确认打印入库计划单的条码标签？\r\n(注意：若入库计划当前处于等待打印标签状态，打印后将进入收货阶段！)", _sCurrentInPlanBillCode.Trim()), "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }
            {
                InDateSelectForm window = new InDateSelectForm();
                if (window.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }
                else
                {
                    PrintBarCodeForm tempwindow = new PrintBarCodeForm(_sCurrentInPlanBillCode, window.SelectedDateTime);
                    tempwindow.ShowDialog();
                    this.InitControl();
                    this.inPlanBillDetailUC.Clear();
                    this.inPlanBillUC.Clear();
                }
            }
        }

        private void btnInput_Click(object sender, EventArgs e)
        {
            //从Excel导入入库计划单
            //1.首先读入列表
             OpenFileDialog oDlg = new OpenFileDialog();
            oDlg.Filter = "*.xls|*.xls";
            if (oDlg.ShowDialog() == DialogResult.OK)
            {
                //打开文件
                FileStream fileTemplate = new FileStream(oDlg.FileName, FileMode.Open, FileAccess.Read);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(fileTemplate);
                //获取工作簿
                HSSFSheet sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet;
                //读取入库单基本信息
                BilInPlanBill ipBill = new BilInPlanBill();
                List<BilInPlanBillDetail> ipPlanBillDetailList = new List<BilInPlanBillDetail>();
                {
                    HSSFRow row = sheet.GetRow(1) as HSSFRow;
                    //采购单号，发货单号
                    string sPO = "";
                    string sSendBillCode = "";
                    if (row.GetCell(2) != null)
                    {
                        sPO = row.GetCell(2).ToString();
                    }
                    if (row.GetCell(6) != null)
                    {
                        sSendBillCode = row.GetCell(5).ToString();
                    }
                    ipBill.POCode = sPO;
                    ipBill.SendBillCode = sSendBillCode;
                }
                {
                    HSSFRow row3 = sheet.GetRow(3) as HSSFRow;
                    HSSFRow row4 = sheet.GetRow(4) as HSSFRow;
                    //采购单号，发货单号
                    string sSenderCode = "";
                    string sSenderName = "";
                    string sReceiverName = "";
                    if (row3.GetCell(6) != null)
                    {
                        sSenderCode = row3.GetCell(6).ToString();
                    }
                    if (row4.GetCell(2) != null)
                    {
                        sReceiverName = row4.GetCell(2).ToString();
                    }
                    if (row4.GetCell(6) != null)
                    {
                        sSenderName = row4.GetCell(6).ToString();
                    }
                    ipBill.SenderCode = sSenderCode;
                    ipBill.SenderName = sSenderName;
                    ipBill.RecieverName = sReceiverName;
                }

                {

                    int iStartRow = 8;
                    HSSFRow row = sheet.GetRow(iStartRow) as HSSFRow;
                    while (row != null && row.GetCell(0) != null && row.GetCell(0).ToString() !="")
                    {
                        string sGoodsCode = "";
                        int iPackage = 0;
                        int iPiece = 0;
                        decimal PiecePrice = 0;
                        string sRemark = "";

                        BilInPlanBillDetail detail = new BilInPlanBillDetail();
                        detail.POCode = ipBill.POCode;

                        //货物编码
                        if (row.GetCell(1) != null)
                        {
                            try
                            {
                                sGoodsCode = row.GetCell(1).ToString();
                            }
                            catch
                            {
                                sGoodsCode = "";
                            }
                        }

                        //整箱
                        if (row.GetCell(6) != null)
                        {
                            try
                            {
                                iPackage = int.Parse(row.GetCell(6).ToString());
                            }
                            catch
                            {
                                iPackage = 0;
                            }
                        }

                        //零数
                        if (row.GetCell(7) != null)
                        {
                            try
                            {
                                iPiece = int.Parse(row.GetCell(7).ToString());
                            }
                            catch
                            {
                                iPiece = 0;
                            }
                        }

                        //单价
                        if (row.GetCell(11) != null)
                        {
                            try
                            {
                                PiecePrice = Convert.ToDecimal(row.GetCell(11).ToString());
                            }
                            catch
                            {
                                PiecePrice = 0;
                            }
                        }

                        //备注
                        if (row.GetCell(13) != null)
                        {
                            try
                            {
                                sRemark = row.GetCell(13).ToString();
                            }
                            catch
                            {
                                sRemark = "";
                            }
                        }

                        detail.GoodsCode = sGoodsCode;
                        detail.GoodsPackageQty = iPackage;
                        detail.GoodsPieceQty = iPiece;
                        detail.PiecePrice = PiecePrice;
                        detail.Remark = sRemark;

                        //判断是否在入库单中存在相同的货物，如存在，则合并
                        bool b = false;
                        for (int j = 0; j < ipPlanBillDetailList.Count; j++)
                        {
                            BilInPlanBillDetail tempDetail = ipPlanBillDetailList[j];
                            if (detail.GoodsCode.Trim() == tempDetail.GoodsCode.Trim())
                            {
                                b = true;
                                tempDetail.GoodsPackageQty += detail.GoodsPackageQty;
                                tempDetail.GoodsPieceQty += detail.GoodsPieceQty;
                            }
                        }

                        if (!b)
                        {
                            ipPlanBillDetailList.Add(detail);
                        }


                        iStartRow++;
                        row = sheet.GetRow(iStartRow) as HSSFRow;
                    }
                    if (ipPlanBillDetailList.Count == 0)
                    {
                        MessageBox.Show("导入的入库计划单为空，请检查！");
                        return;
                    }
                    //判断数据的合理性
                    ServiceReferenceZwms.ServiceZwmsClient client
                        = new ServiceReferenceZwms.ServiceZwmsClient();
                    string sIPCode = client.InitInPlanBillCode();
                    bool bHave = true;
                    List<string> unHaveGoodsList = new List<string>();
                    foreach (BilInPlanBillDetail detail in ipPlanBillDetailList)
                    {
                        BaseGood queryRecord = new BaseGood();
                        queryRecord.Id = detail.GoodsCode;
                        queryRecord = client.QueryGoods(queryRecord);
                        if (queryRecord != null)
                        {
                            detail.GoodsName = queryRecord.GoodsName;
                            detail.GoodsSpec = queryRecord.Specs;
                            detail.IBPlanCode = sIPCode;
                            detail.SingleGoodsCode = queryRecord.SingleGoodsCode;
                            detail.GoodsQty = detail.GoodsPackageQty * detail.GoodsSpec + detail.GoodsPieceQty;
                        }
                        else
                        {
                            bHave = false;
                            detail.GoodsName = "";
                            unHaveGoodsList.Add(detail.GoodsCode);
                        }
                    }
                    if (!bHave)
                    {
                        string sMessage = "";
                        for (int i = 0; i < unHaveGoodsList.Count; i++)
                        {
                            sMessage += string.Format("{0}.{1}\r\n", i + 1, unHaveGoodsList[i]);
                        }
                        MessageBox.Show(string.Format("下列商品货物列表中不存在,\r\n货物编码：\r\n{0}请检查入库计划列表！", sMessage));
                    }
                    else
                    {
                        //入库计划单
                        ipBill.Id = sIPCode;
                        ipBill.BillStatus = "0";
                        ipBill.IBType = "0";
                        ipBill.IsDeactived = "0";
                        ipBill.PlanReceiveDate = "";
                        ipBill.WarehouseCode = MainForm.CurrentUser.WarehouseCode;
                        ipBill.WarehouseName = MainForm.CurrentUser.WarehouseName;
                        ipBill.Remark = "";
                        ipBill.Vehicle = "";
                        ipBill.CreateUserCode = MainForm.CurrentUser.Id;
                        ipBill.CreateUserName = MainForm.CurrentUser.UserName;
                        DateTime dtNow = client.GetCurrentSystemTime();
                        ipBill.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                        ipBill.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                        //
                        IPBillDetailInputForm window = new IPBillDetailInputForm(ipBill, ipPlanBillDetailList);
                        if (window.ShowDialog() == DialogResult.OK)
                        {
                            
                            if (client.AddInPlanBill(ipBill))
                            {
                                SysLogWriter.WriteOptLog("导入入库计划单", ipBill.Id, "增");
                                bool bRet = true;
                                foreach (BilInPlanBillDetail detailrec in ipPlanBillDetailList)
                                {
                                    if (!client.AddInPlanBillDetail(detailrec))
                                    {
                                        bRet = false;
                                    }
                                }
                                if (bRet)
                                {
                                    MessageBox.Show("成功！");
                                }
                            }
                            else
                            {
                                MessageBox.Show("添加入库计划失败！");
                                return;
                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}
